<?php

/**
 * Table/SiteHoraireTypeTable.php
 */

namespace RHX\Model\Table;

use Zend\Db\Adapter\Adapter;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\ResultSet\ResultSet;
use RHX\Model\Object\SiteHoraireType;
use Zend\Db\Sql\Select;
use  Zend\Db\Sql\Where;

class SiteHoraireTypeTable extends AbstractTableGateway{
    protected $table = 'rh_site_horaire_type';
    
    /**
     * Constructeur
     * 
     * @param \Zend\Db\Adapter\Adapter $adapter
     */
    public function __construct(Adapter $adapter) {
	$this->adapter			 = $adapter;
	$this->resultSetPrototype	 = new ResultSet();
	$this->resultSetPrototype->setArrayObjectPrototype(new SiteHoraireType());
	$this->initialize();
    }
    
    /**
     * Tous les enregistrements
     * 
     * @param string $order
     * @return ResultSet
     */
    public function getAll($order = 'libelle ASC'){
	return $this->select(function(Select $select)use($order){
	    $select->order($order);
	});
    }
    
    /**
     * Récupérer un enregistrement
     * 
     * @param int $id
     * @return SiteHoraireType|null
     */
    public function get($id){
	$row = $this->select(array('id' => $id))->current();
	if(!$row){
	    return NULL;
	}
	return $row;
    }
    
    /**
     * Effacer un enregistrement
     * 
     * @param int $id
     * @return int
     */
    public function erase($id){
	return $this->delete(array('id' => $id));
    }
    
    /**
     * Sauvegarder un enregistrement
     * 
     * @param \RHX\Model\Object\SiteHoraireType $type
     * @return int
     */
    public function save(SiteHoraireType $type){
	$data = array(
	    'libelle' => $type->libelle,
	);
	$id = (int)$type;
	if($id == 0){
	    $this->insert($data);
	    return $this->getLastInsertValue();
	}elseif(!is_null($this->get($id))){
	    $this->update($data, array('id' => $id));
	    return $id;
	}else{
	    return 0;
	}
    }
}
